package pe.gob.osinergmin.interfase_siaf.dac.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import pe.gob.osinergmin.interfase_siaf.dac.dao.MetaDao;
import pe.gob.osinergmin.interfase_siaf.dac.domain.Meta;

@Repository("metaDao")
public class MetaDaoImpl implements MetaDao {
	
	
	private NamedParameterJdbcTemplate jdbcParameterTemplateData;	
	
	private JdbcTemplate jdbcTemplateData;	
	 
	@Autowired
	public void SetDataSource(DataSource dataSourceData){	
		 this.jdbcTemplateData = new JdbcTemplate(dataSourceData);
		 this.jdbcParameterTemplateData = new NamedParameterJdbcTemplate(dataSourceData);
	}
	 
	 
	 
	 /**DECLARACION DE SQL**/
	private static final String SQL_COUNT_SEC_FUNC_META=
			" SELECT COUNT(sec_func) FROM meta where sec_func=:secFunc AND "
			+ " sec_ejec=:secEjec AND ano_eje =:anoEje ";

	
	private static final String SQL_SELECT_META ="SELECT sec_func, nombre FROM meta ";
	private static final String SQL_SELECT_SEC_FUNC_META ="SELECT sec_func, nombre FROM meta WHERE sec_func =:secFunc";
	private static final String SQL_SELECT_ANIO_EJEC_META ="SELECT sec_func, nombre FROM meta WHERE ano_eje =:anoEje";
	private static final String SQL_SELECT_META_PARAMETERS ="SELECT sec_func, nombre FROM meta WHERE sec_func =:secFunc AND ano_eje =:anoEje";
    
	
	/**IMPLEMENTACION DE METODOS**/	
	 @Override
	 public int verificarExiteSecFunc(Meta m) throws Exception{
		 SqlParameterSource parameters = new BeanPropertySqlParameterSource(m);
		 return this.jdbcParameterTemplateData.queryForInt(SQL_COUNT_SEC_FUNC_META, parameters);
	 }
	 
	 
	 @Override
	 public List<Meta> listarMetaParameter(Meta meta) throws Exception{		
		 SqlParameterSource parameters = new BeanPropertySqlParameterSource(meta);
		 RowMapper<Meta> lista = ParameterizedBeanPropertyRowMapper.newInstance(Meta.class);
		 if(StringUtils.isNoneBlank(meta.getAnoEje()) && StringUtils.isNoneBlank(meta.getSecFunc())){ 
			 return this.jdbcParameterTemplateData.query(SQL_SELECT_META_PARAMETERS, parameters, lista);	
		 }else if(StringUtils.isNoneBlank(meta.getAnoEje())){ 
			 return this.jdbcParameterTemplateData.query(SQL_SELECT_ANIO_EJEC_META, parameters, lista);	
		 }else if(StringUtils.isNoneBlank(meta.getSecFunc())){
			 return this.jdbcParameterTemplateData.query(SQL_SELECT_SEC_FUNC_META, parameters, lista);	
		 }else{
			 return null;
		 }			
	 }
	 
	 @Override
	 public List<Meta> listarMetas() throws Exception{		
		 RowMapper<Meta> lista = ParameterizedBeanPropertyRowMapper.newInstance(Meta.class);
		 return this.jdbcTemplateData.query(SQL_SELECT_META, lista);
	 }

}
